package zzg.study.server.handler;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import zzg.study.util.SessionUtil;

/**
 * @Classname AuthHandler
 * @Description 用户认证处理
 * @Date 2023/12/9 20:35
 * @Author zhuzhenguo
 */
@ChannelHandler.Sharable
public class AuthHandler extends ChannelInboundHandlerAdapter {

    public static final AuthHandler INSTANCE = new AuthHandler();

    private AuthHandler() {

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (!SessionUtil.hasLogin(ctx.channel())) {
            ctx.channel().close();
        } else {
            // 登录过校验一次就行了，连接没有断开，不需要每次都进行校验。这里实现逻辑上的删除
            ctx.pipeline().remove(this);
            super.channelRead(ctx, msg);
        }
    }
}
